﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HRM.HRMServiceProxy;

namespace HRM
{
    public partial class frmUserBranchList : FormBase
    {
        UserClient _wcfUser;
        DataSet _ds;

        public frmUserBranchList()
        {
            InitializeComponent();
            _wcfUser = Service.User();

            BuildVoucherGrid();
            LoadData();
            txtTuKhoa.Focus();
        }

        public DialogResult Show_Form()
        {
            return this.ShowDialog();
        }

        private void BuildVoucherGrid()
        {
            UISetting.AddColumn(ref dgvList, "Branch_ID", "Branch_ID", "", 1, false, true);
            UISetting.AddColumn(ref dgvList, "Branch_Code", "Mã đơn vị", "", 80, true, true);
            UISetting.AddColumn(ref dgvList, "Branch_Name", "Tên đơn vị", "", 405, true, true);
        }
        private void LoadData()
        {
            int Type = (Program.isAdmin || Program.strUserName.ToLower() == "vvtuy") ? 0 : 1;
            _ds = _wcfUser.UserBranch_GetListChuyenQuan(Type, Program.strUserName);

            dgvList.Rows.Clear();
            for (int i = 0; i < _ds.Tables[0].Rows.Count; i++)
            {
                dgvList.Rows.Insert(i, 1);
                dgvList.Rows[i].Cells["Branch_ID"].Value = _ds.Tables[0].Rows[i]["Branch_ID"].ToString();
                dgvList.Rows[i].Cells["Branch_Code"].Value = _ds.Tables[0].Rows[i]["Branch_Code"].ToString();
                dgvList.Rows[i].Cells["Branch_Name"].Value = _ds.Tables[0].Rows[i]["Branch_Name"].ToString();
            }
        }

        private void dgvList_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvList.SelectedRows.Count > 0)
            {
                Program.intBranchID = Convert.ToInt32(dgvList.SelectedRows[0].Cells["Branch_ID"].Value);
                Service.LoadSystem();
                //CloseAllOpenForm();
                //MessageBox.Show("Bạn đang đăng nhập với tài khoản của đơn vị: " + Program.strBranch);
                this.Close();
            }
        }

        private void CloseAllOpenForm()
        {
            FormCollection fc = Application.OpenForms;
            if (fc != null && fc.Count > 0)
            {
                for (int i = 1; i < fc.Count; i++)
                {
                    //if (fc[i] != null && fc[i].IsDisposed != true)
                    if (fc[i] != null)
                    {
                        fc[i].Close();
                        fc[i].Dispose();
                    }
                }
            }
        }

        private void dgvList_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                dgvList_CellContentDoubleClick(null, null);
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            FormGlobals.App_ProgressShow(delegate(){
                dgvList.Rows.Clear();
                string keyWord = txtTuKhoa.Text.Trim();
                int Counter = 0;

                for (int i = 0; i < _ds.Tables[0].Rows.Count; i++)
                {
                    string BranchCode = _ds.Tables[0].Rows[i]["Branch_Code"].ToString();
                    string BranchName = _ds.Tables[0].Rows[i]["Branch_Name"].ToString();

                    if (keyWord == "" || BranchCode.ToLower().Contains(keyWord.ToLower()) || BranchName.ToLower().Contains(keyWord.ToLower()))
                    {
                        dgvList.Rows.Insert(Counter, 1);
                        dgvList.Rows[Counter].Cells["Branch_ID"].Value = _ds.Tables[0].Rows[i]["Branch_ID"].ToString();
                        dgvList.Rows[Counter].Cells["Branch_Code"].Value = _ds.Tables[0].Rows[i]["Branch_Code"].ToString();
                        dgvList.Rows[Counter].Cells["Branch_Name"].Value = _ds.Tables[0].Rows[i]["Branch_Name"].ToString();
                        Counter++;
                    }
                }
            }, "Tìm kiếm...");
        }

        private void txtTuKhoa_KeyUp(object sender, KeyEventArgs e)
        {
            
            if (e.KeyCode.Equals(Keys.Enter))
            {
                string txtPreSearch = Convert.ToString(txtTuKhoa.Tag);

                btnSearch_Click(null, null);
                txtTuKhoa.Tag = txtTuKhoa.Text;

                //Enter 2 lần và chỉ có 1 đơn vị thì tự động chọn
                if (txtPreSearch != "" && txtPreSearch == txtTuKhoa.Text 
                    //&& dgvList.Rows.Count == 1
                    )
                {
                    FormGlobals.App_ProgressShow(delegate()
                    {
                        Program.intBranchID = Convert.ToInt32(dgvList.Rows[0].Cells["Branch_ID"].Value);
                        Service.LoadSystem();
                    }, "Đang tải dữ liệu của nhân sự");
                    this.Close();
                }
            }
        }

        private void frmUserBranchList_Activated(object sender, EventArgs e)
        {
            txtTuKhoa.Focus();
        }
    }
}
